package com.example.lib_mvp.views;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class StarView extends View {

    private Paint paint;
    private float scale = 1.0f; // 缩放比例
    private float x = 0; // x坐标
    private float y = 0; // y坐标

    public StarView(Context context) {
        super(context);
        init();
    }

    public StarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public StarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.YELLOW);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 保存当前Canvas状态
        canvas.save();

        // 平移到(x, y)坐标
        canvas.translate(x, y);

        // 缩放
        canvas.scale(scale, scale);

        // 绘制四角星
        drawStar(canvas, 0, 0, 100, 50);

        // 恢复Canvas状态
        canvas.restore();
    }

    private void drawStar(Canvas canvas, float x, float y, float outerRadius, float innerRadius) {
        int numPoints = 4;
        double angle = Math.PI / numPoints;

        float[] points = new float[numPoints * 4];
        for (int i = 0; i < numPoints * 2; i++) {
            float px = (float) (x + (i % 2 == 0 ? outerRadius : innerRadius) * Math.cos(angle * i - Math.PI / 2));
            float py = (float) (y + (i % 2 == 0 ? outerRadius : innerRadius) * Math.sin(angle * i - Math.PI / 2));
            points[i * 2] = px;
            points[i * 2 + 1] = py;
        }

        canvas.drawLines(points, paint);
    }

    // 开始动画
    public void startAnimation() {
        // 缩放动画
        ValueAnimator scaleAnimator = ValueAnimator.ofFloat(1.0f, 0.5f);
        scaleAnimator.setDuration(1000);
        scaleAnimator.setRepeatCount(ValueAnimator.INFINITE);
        scaleAnimator.setRepeatMode(ValueAnimator.REVERSE);
        scaleAnimator.addUpdateListener(animation -> {
            scale = (float) animation.getAnimatedValue();
            invalidate();
        });

        // 移动动画
        ValueAnimator translateAnimator = ValueAnimator.ofFloat(0, -200);
        translateAnimator.setDuration(1000);
        translateAnimator.setRepeatCount(ValueAnimator.INFINITE);
        translateAnimator.setRepeatMode(ValueAnimator.REVERSE);
        translateAnimator.addUpdateListener(animation -> {
            y = (float) animation.getAnimatedValue();
            invalidate();
        });

        // 创建动画集
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(scaleAnimator, translateAnimator);
        animatorSet.start();
    }
}